package com.smartthinking.CICPNL.home.activity;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;
import com.smartthinking.CICPNL.home.objects.clsErrorInternet;
import com.smartthinking.CICPNL.home.objects.clsImages;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class Splash2 extends Activity {
	AnimationSet animationSet;
	ImageView imagen;
	LinearLayout layout;
	private String PUT_IMG;
	private int PUT_TIME;
	private String PUT_PAT;

	public App_ImageLoader imageLoader;
	private ArrayList<Dictionary> Actividades;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.splash);
		
		Bundle extras = getIntent().getExtras();
		this.PUT_IMG = extras.getString("PUT_IMG");
		this.PUT_PAT = extras.getString("PUT_PAT");
		this.PUT_TIME = Integer.parseInt(extras.getString("PUT_TIME"))*1000;

		imagen = (ImageView) findViewById(R.id.iv_main_splash);
		layout = (LinearLayout) findViewById(R.id.inicio);

		configurarAnimacion();

		imagen.startAnimation(animationSet);
	}
	
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Checks the orientation of the screen
	}

	public void configurarAnimacion() {
		animationSet = new AnimationSet(true);

		Animation fadein = AnimationUtils.loadAnimation(this, R.anim.fadein);
		fadein.setDuration(500);

		Animation fadeout = AnimationUtils.loadAnimation(this, R.anim.fadeout);
		fadeout.setDuration(500);
		fadeout.setStartOffset(PUT_TIME); // Numero de milisegundos que quedara la
										// imagen antes de desaparecer
		//animationSet.addAnimation(fadein);
		animationSet.addAnimation(fadeout);
		animationSet.setStartOffset(0);
		animationSet.setAnimationListener(new AnimationListener() {
			public void onAnimationEnd(Animation animation) {
				layout.removeAllViews();
				finish();
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationStart(Animation animation) {
				//imagen.setImageResource(R.drawable.default2);
	            Bitmap bmp = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/Pediatria/" + PUT_IMG);
	            imagen.setImageBitmap(bmp);
			}
		});
	}

	public void goPatrocinador(View v){
		Intent intent = new Intent(this, PatrocinadoresDetalle.class);
		Bundle bund = new Bundle();
		bund.putString("idpatrocinador",PUT_PAT);
		intent.putExtras(bund);
		startActivity(intent);
		finish();
	}
}